package www.woniuxy.com.class112.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
//import www.woniuxy.com.class112.entity.UserInfos;
import org.springframework.web.multipart.MultipartFile;
import www.woniuxy.com.class112.dto.QueryUserInformationDto;
import www.woniuxy.com.class112.entity.UserInfos;
import www.woniuxy.com.class112.form.ModifyUserInformationForm;
import www.woniuxy.com.class112.form.userInfo.*;
import www.woniuxy.com.class112.form.AdminRegistrationForm;
import www.woniuxy.com.class112.param.AdminRegistrationParam;
import www.woniuxy.com.class112.param.ModifyUserInformationParam;
import www.woniuxy.com.class112.param.userInfo.*;
import www.woniuxy.com.class112.service.UserInfoService;
import www.woniuxy.com.class112.util.JwtUtil;
import www.woniuxy.com.class112.util.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/userInfo")
@Api(tags = "管理员和用户相关接口")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;
    @PostMapping("/userLogin")
    @ApiOperation("用户登录")
    public Result userLogin(@RequestBody UserLoginForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.userLogin(BeanUtil.toBean(form, UserLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("登录成功",userInfo.getRoleId());
    }

    @PostMapping("/adminRegistration")
    @ApiOperation("临时注册测试方案")
    public Result adminRegistration(@RequestBody AdminRegistrationForm form) throws Exception {
        userInfoService.adminRegistration(BeanUtil.toBean(form, AdminRegistrationParam.class));
        return Result.success("注册成功",null);
    }

    @GetMapping("/LoginVerificationCode")
    @ApiOperation("用户登录验证码发送")
    public Result LoginVerificationCode(String phone) throws Exception {
        userInfoService.LoginVerificationCode(phone);
        return Result.success("登录验证码发送成功",null);
    }

    @PostMapping("/loginWithPhoneNumber")
    @ApiOperation("用户登录验证码核验")
    public Result loginWithPhoneNumber(@RequestBody LoginUserInfoCodeForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.loginWithPhoneNumber(BeanUtil.toBean(form, LoginUserInfoCodeParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("登录验证码核验成功",userInfo.getRoleId());
    }

    @GetMapping("/registrationVerificationCode")
    @ApiOperation("用户注册验证码发送")
    public Result registrationVerificationCode(String phone) throws Exception {
        userInfoService.registrationVerificationCode(phone);
        return Result.success("注册验证码发送成功",null);
    }

    @PostMapping("/userMobileNumberRegistration")
    @ApiOperation("用户注册验证码核验")
    public Result userMobileNumberRegistration(@RequestBody RegisterUserInfoCodeForm form) throws Exception {
        userInfoService.userMobileNumberRegistration(BeanUtil.toBean(form, RegisterUserInfoCodeParam.class));
        return Result.success("注册验证码核验成功",null);
    }

    @PostMapping("/superAdminLogin")
    @ApiOperation("基层工作人员登录")
    public Result superAdminLogin(@RequestBody UserLoginForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.superAdminLogin(BeanUtil.toBean(form, UserLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("登录成功",null);
    }
    @GetMapping("/superAdminVerificationCodeSending")
    @ApiOperation("基层工作人员发送登录验证码")
    public Result superAdminVerificationCodeSending(String phone) throws Exception {
        userInfoService.superAdminVerificationCodeSending(phone);
        return Result.success("登录验证码发送成功",null);
    }
    @PostMapping("/superAdminLoginCode")
    @ApiOperation("基层工作人员登录验证码核验")
    public Result superAdminLoginCode(@RequestBody LoginUserInfoCodeForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.superAdminLoginCode(BeanUtil.toBean(form, LoginUserInfoCodeParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("登录验证码核验成功",null);
    }

    @PostMapping("/agentLogin")
    @ApiOperation("特派员登录接口")
    public Result agentLogin(@RequestBody UserLoginForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.agentLogin(BeanUtil.toBean(form, UserLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("特派员登录成功",null);
    }
    @GetMapping("/agentLoginCodeSending")
    @ApiOperation("特派员发送登录验证码")
    public Result agentLoginCodeSending(String phone) throws Exception {
        userInfoService.agentLoginCodeSending(phone);
        return Result.success("登录验证码发送成功",null);
    }

    @PostMapping("/agentLoginCode")
    @ApiOperation("基层工作人员登录验证码核验")
    public Result agentLoginCode(@RequestBody LoginUserInfoCodeForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.agentLoginCode(BeanUtil.toBean(form, LoginUserInfoCodeParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("登录验证码核验成功",null);
    }
    @PostMapping("/adminLogin")
    @ApiOperation("管理员登录")
    public Result adminLogin(@RequestBody UserLoginForm form, HttpServletResponse response) throws Exception {
        UserInfos userInfo = userInfoService.adminLogin(BeanUtil.toBean(form, UserLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);
        String token = JwtUtil.createToken(map, 24 * 60);
        response.setHeader("token",token);
        return Result.success("管理员登录成功",null);
    }
    @GetMapping("/selectUser")
    @ApiOperation("查询用户")
    public Result selectUser(){
        return Result.success("查询成功",userInfoService.selectUser());
    }
    @PostMapping("/selectAdminAccout")
    @ApiOperation("管理员查询")
    public Result selectAdminAccout(@RequestBody SelectAdminAccoutForm form){
        System.out.println(form);
        return Result.success("查询成功管理人员",userInfoService.selectAdminAccout(BeanUtil.toBean(form, SelectAdminAccoutParam.class)));
    }
    @GetMapping("/delistAccout")
    @ApiOperation("禁用管理员")
    public Result delistAccout(Long id)throws Exception{
        userInfoService.delistAccout(id);
        return Result.success("禁用管理员成功",null);
    }
    @GetMapping("/enableAccout")
    @ApiOperation("启用管理员")
    public Result enableAccout(Long id)throws Exception{
        userInfoService.enableAccout(id);
        return Result.success("启用管理员成功",null);
    }
    @GetMapping("/deleteAccout")
    @ApiOperation("删除管理员")
    public Result deleteAccout(Long id)throws Exception{
        userInfoService.deleteAccout(id);
        return Result.success("删除管理员成功",null);
    }
    @PostMapping("/addAccout")
    @ApiOperation("添加管理员")
    public Result addAccout(@RequestBody AddAccoutForm form) throws Exception {
        userInfoService.addAdminAccout(BeanUtil.toBean(form, AddAccoutParam.class));
        return Result.success("添加管理员成功", null);
    }

    @PostMapping("/modifyUserInformation")
    @ApiOperation("修改用户信息")
    public Result modifyUserInformation(@RequestBody ModifyUserInformationForm form,@RequestHeader String token) throws Exception {
        Long userId = JwtUtil.getUserId(token);
        userInfoService.modifyUserInformation(BeanUtil.toBean(form, ModifyUserInformationParam.class),userId);
        return Result.success("修改成功",null);
    }
    @GetMapping("/queryUserInformation")
    @ApiOperation("查询用户信息")
    public Result queryUserInformation(@RequestHeader String token){
        Long userId = JwtUtil.getUserId(token);
        QueryUserInformationDto dto = userInfoService.queryUserInformation(userId);
        return Result.success("查询成功",dto);
    }

    @GetMapping("/searchForProfilePicture")
    @ApiOperation("查询头像")
    public Result searchForProfilePicture(@RequestHeader String token){
        Long userId = JwtUtil.getUserId(token);
        String url = userInfoService.searchForProfilePicture(userId);
        return Result.success("查询成功",url);
    }

    @PostMapping("/updateProfilePicture")
    @ApiOperation("更新头像")
    public Result updateProfilePicture(@RequestParam("file") MultipartFile file,@RequestHeader String token) throws Exception {
        Long userId = JwtUtil.getUserId(token);
        userInfoService.updateProfilePicture(file,userId);
        return Result.success("更新成功",null);
    }
}
